package org.aptech.hdax.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aptech.hdax.pojo.Attendence;
import org.aptech.hdax.pojo.Department;
import org.aptech.hdax.service.AttendenceService;
import org.aptech.hdax.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
@RequestMapping("/attend")
public class AttendenceController {
	@Autowired
	private AttendenceService attendenceService;
	@Autowired
	private DepartmentService departmentService;
	
	@RequestMapping(value = "/list", produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public Map<String, Object> list(Attendence attendence, Integer page, Integer limit) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Attendence> pager = PageHelper.startPage(page, limit);
		map.put("data", attendenceService.getList(attendence));
		map.put("code", 0);
		map.put("count", pager.getTotal());
		return map;
	}
	
	@RequestMapping(value = "/listdept", produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public List<Department> listdept() throws Exception {
		return departmentService.getAll();
	}
	
	@PostMapping("/add")
	public String add(Attendence attendence) throws Exception {
		attendenceService.add(attendence);
		return "redirect:/index.jsp";
	}
	
	@RequestMapping(value = "/delete", produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public Map<String, Object> delete(Integer[] ids) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		attendenceService.batchDelete(ids);
		
		map.put("result", true);
		
		return map;
	}
		
}












